package cn.itcast.myapplication.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.itcast.myapplication.R;
import cn.itcast.myapplication.db.MyDataBase;
import cn.itcast.myapplication.db.UserModel;

public class RoomActivity extends AppCompatActivity  {

    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.input_et)
    EditText inputET;

    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.result_tv)
    TextView resultTV;

    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.set_btn)
    Button set_btn;

    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.get_btn)
    Button get_btn;

    //创建
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room);

        //绑定，不要忘了绑定
        ButterKnife.bind(this);

        set_btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                setUser();//添加数据

            }

        });


        get_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                getUser();//数据查询

            }

        });

    }


    void setUser() {

            String name = inputET.getText().toString();//输入用户名
            UserModel userModel = new UserModel();
            userModel.setName(name);//设置名字
            userModel.setAddress("山东青岛");//设置地址
            userModel.setAge(99);//设置年龄
            userModel.setPhone("15006512345");//设置电话号码
            MyDataBase.getInstance(this).userDao().insertUser(userModel);//添加数据

    }


    void getUser() {

        List<UserModel> users = MyDataBase.getInstance(this).userDao().getAllUsers();//查询获取所有的数据

        if (users != null && users.size() > 0) {

            resultTV.setText(users.get(0).getName());

        }

    }

}